package com.ccrfid.rmc6.service.video;

import com.ccrfid.rmc6.appconfig.exception.pojo.AppException;
import com.ccrfid.rmc6.appconfig.exception.pojo.CustomErrors;
import com.ccrfid.rmc6.entity.video.VideoPlatformEntity;
import com.ccrfid.rmc6.pojo.dto.VideoPlatformDto;
import com.ccrfid.rmc6.repository.video.VideoPlatformRepository;
import com.ccrfid.rmc6.util.NetUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import javax.ws.rs.core.Response;
import java.util.List;

@Service
@Transactional
public class VideoPlatformServiceImpl implements VideoPlatformService {
    private static final Logger logger = LoggerFactory.getLogger(VideoPlatformServiceImpl.class);
    @Autowired
    private VideoPlatformRepository videoPlatformRepository;
    @Autowired
    private VideoSyncService videoSyncService;

    @Override
    public List<VideoPlatformEntity> getAllVideoPlatforms() {
        return videoPlatformRepository.getAllBy();
    }

    @Override
    public VideoPlatformEntity getVideoPlatformById(Long platformId) {
        VideoPlatformEntity entity = videoPlatformRepository.getById(platformId);
        return entity;
    }

    @Override
    public VideoPlatformDto getVideoPlatfromLinkageInfo(Long platformId, String clientIpAddr) {
        VideoPlatformEntity platformEntity = videoPlatformRepository.getById(platformId);
        if (platformEntity != null) {
            String ip = NetUtil.getMatchIPAddr(clientIpAddr, platformEntity.getIp(), platformEntity.getIp2());
            VideoPlatformDto dto = new VideoPlatformDto();
            dto.setVersion(videoSyncService.getIVersion(platformEntity.getPlatformVersion()));
            dto.setIp(ip);
            dto.setPort(platformEntity.getPort());
            dto.setUsername(platformEntity.getUsername());
            dto.setPassword(platformEntity.getPasswd());
            return dto;
        } else {
            return null;
        }
    }

    @Override
    public VideoPlatformEntity createVideoPlatform(VideoPlatformEntity entity) throws AppException {
        return videoPlatformRepository.save(entity);
    }

    @Override
    public VideoPlatformEntity updateVideoPlatform(Long platformId, VideoPlatformEntity entity) throws AppException {
        if (!platformId.equals(entity.getId())) {
            throw new AppException(Response.Status.BAD_REQUEST.getStatusCode(),
                    CustomErrors.different_args.getCode(), CustomErrors.different_args.getReason(), "");
        }
        if (!videoPlatformRepository.existsById(platformId)) {
            throw new AppException(Response.Status.NOT_FOUND.getStatusCode(),
                    CustomErrors.resource_not_found.getCode(), CustomErrors.resource_not_found.getReason(), "");
        }
        return videoPlatformRepository.save(entity);
    }

    @Override
    public Boolean deleteVideoPlatform(Long id) throws AppException {
        if (!videoPlatformRepository.existsById(id)) {
            throw new AppException(Response.Status.NOT_FOUND.getStatusCode(),
                    CustomErrors.resource_not_found.getCode(), CustomErrors.resource_not_found.getReason(), "");
        }
        videoPlatformRepository.deleteById(id);
        return true;
    }

    @Override
    public Boolean syncPlatformNode(Long platformId) throws AppException {
        VideoPlatformEntity platformEntity = videoPlatformRepository.getById(platformId);
        if (platformEntity == null) {
            throw new AppException(Response.Status.NOT_FOUND.getStatusCode(),
                    CustomErrors.resource_not_found.getCode(), CustomErrors.resource_not_found.getReason(), "");
        }

        videoSyncService.syncVideoPlatform(platformEntity);
        return true;
    }
}
